home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / lang / SmallEiffel.lha / SmallEiffel / lib_std / std_file_read.e < prev    next >
Text File  |  1998-12-22  |  3KB  |  171 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://www.loria.fr/SmallEiffel
  11. --
  12. class STD_FILE_READ 
  13. --
  14. -- Basic input facilities to read a named file on the disc.
  15. --
  16. -- Note : most features are common with STD_INPUT so you can 
  17. -- test your program on the screen first and then, just changing
  18. -- of instance (STD_INPUT/STD_FILE_READ), doing the same in a file.
  19. --
  20.    
  21. inherit INPUT_STREAM;
  22.    
  23. creation 
  24.    connect_to, make
  25.  
  26. feature
  27.    
  28.    path: STRING;
  29.      -- Not Void when connected to the corresponding file 
  30.      -- on the disk.
  31.    
  32. feature {INPUT_STREAM}
  33.    
  34.    input_stream: POINTER;
  35.  
  36. feature {NONE}
  37.  
  38.    memory: INTEGER;
  39.      -- Memory of the last available user's value.
  40.  
  41. feature 
  42.    
  43.    make is
  44.       do
  45.       end;
  46.    
  47.    connect_to(new_path: STRING) is
  48.       require
  49.      not is_connected;
  50.      not new_path.empty
  51.       local
  52.      p: POINTER;
  53.       do
  54.      p := new_path.to_external;
  55.      input_stream := sfr_open(p);
  56.      if input_stream.is_not_void then
  57.         push_back_flag := false;
  58.         memory := (' ').code;
  59.         path := new_path;
  60.      end;
  61.       end;
  62.  
  63. feature    
  64.  
  65.    disconnect is
  66.       require
  67.      is_connected
  68.       do
  69.      fclose(input_stream); 
  70.      path := Void;
  71.       end;
  72.    
  73.    is_connected: BOOLEAN is
  74.       do
  75.      Result := path /= Void;
  76.       end;
  77.    
  78.    read_character is
  79.       do
  80.      if push_back_flag then
  81.         push_back_flag := false;
  82.      else
  83.         memory := read_byte(input_stream);
  84.      end;
  85.       end;
  86.  
  87.    last_character: CHARACTER is
  88.       do
  89.      Result := memory.to_character;
  90.       end;
  91.  
  92.    unread_character is
  93.       do
  94.      push_back_flag := true;
  95.       end;
  96.  
  97.    end_of_input: BOOLEAN is
  98.       do
  99.      if not push_back_flag then
  100.         Result := memory = eof_code;
  101.      end;
  102.       end;
  103.  
  104. feature
  105.  
  106.    read_line_in(str: STRING) is
  107.       local
  108.      mem: INTEGER;
  109.       do
  110.      read_character;
  111.      from  
  112.         mem := memory;
  113.      until
  114.         mem = eof_code 
  115.            or else 
  116.         mem = ('%N').code
  117.            or else
  118.         mem = ('%R').code
  119.      loop
  120.         str.extend(mem.to_character);
  121.         mem := read_byte(input_stream);
  122.      end;
  123.      memory := mem;
  124.      if mem = ('%R').code then
  125.         read_character;
  126.         if last_character /= '%N' then
  127.            unread_character;
  128.         end;
  129.      end;
  130.       end;
  131.  
  132. feature {FILE_TOOLS}
  133.  
  134.    same_as(other: like Current): BOOLEAN is
  135.       require
  136.      is_connected;
  137.      other.is_connected
  138.       local
  139.      is1, is2: like input_stream;
  140.      c1, c2: INTEGER;
  141.       do
  142.      from
  143.         is1 := input_stream;
  144.         is2 := other.input_stream;
  145.      until
  146.         c1 /= c2 or else c1 = eof_code
  147.      loop
  148.         c1 := read_byte(is1);
  149.         c2 := read_byte(is2);
  150.      end
  151.      Result :=  c1 = c2;
  152.      disconnect;
  153.      other.disconnect;
  154.       ensure
  155.      not is_connected;
  156.      not other.is_connected
  157.       end;
  158.  
  159. feature {NONE}
  160.  
  161.    sfr_open(path_pointer: POINTER): POINTER is
  162.       external "SmallEiffel"
  163.       end;
  164.  
  165.    fclose(stream_pointer : POINTER) is
  166.       external "C_InlineWithoutCurrent"
  167.       end;
  168.  
  169. end -- STD_FILE_READ 
  170.  
  171.